﻿package test.liu
{
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DMovie;
	import _Pan3D.particle.FireParticle;
	import _Pan3D.particle.DiffuseParticle;
	import _Pan3D.particle.UVParticle;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.BaseShader;
	import _Pan3D.program.shaders.DiffuseShader;
	import _Pan3D.program.shaders.DynmicUVShader;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.texture.TextureManager;
	
	import _me.Config;
	import _me.KeyControl;
	import _me.MathCoreWillDele;
	import _me.Scene_data;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	import com.bit101.components.ColorChooser;
	import com.bit101.components.FPSMeter;
	import com.bit101.components.HSlider;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.RadioButton;
	import com.bit101.components.Text;
	
	import flash.display.*;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;

	[SWF(backgroundColor="#FFFFFF",width="1024", height="600", frameRate="60")]
	public class indexCombine extends MovieClip {
		private var keyControl : KeyControl = new KeyControl();
		private var container : Display3DContainer;
		private var projMatrix : PerspectiveMatrix3D;
		private var program : Program3D;
		private var guaiwu : Display3DMovie;
		private var particle:DiffuseParticle;
		private var particlelist:Vector.<FireParticle>

		private var wnum:int = 3;
		private var hnum:int = 3;

		public function indexCombine() : void {
			Scene_data.cam3D.distance = 5;
			Scene_data.focus3D.angle_x = 0;
			addEvents();
		}

		

		private function addEvents() : void {
			this.addEventListener(Event.ADDED_TO_STAGE, addToStage);
		}

		private function addToStage(event : Event) : void {
			Config.init(stage, _config_end);
			addView();
			keyControl.init(stage);
		}

		private function _config_end() : void {
			var context : Context3D = Scene_data.context3D;
			Program3DManager.getInstance().initReg();
			
			program = Program3DManager.getInstance().getProgram(DiffuseShader.DIFFUSESHADER);
			makeScene();
		}

		private function makeScene() : void {
			var context : Context3D = Scene_data.context3D;
			context.setDepthTest(false, Context3DCompareMode.LESS);
			context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE);
			//context.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ONE);
			//context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			TextureManager.getInstance().init(context);
			
			particle = new DiffuseParticle(context);
			new FPSMeter(this);
			particle.scale = 2;
			particle.allTime = 100;
			this.addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		private var baseparticle:FireParticle;
		private var rx:int;
		private function enterFrame(event : Event) : void {
			//Scene_data.cam3D.distance = 5;
			MathCoreWillDele._catch_cam(Scene_data.cam3D, Scene_data.focus3D);
			Scene_data.focus3D.angle_y = mouseX;
			rx++;
			//particle.x = mouseX/100
			//trace(mouseX)
			//Scene_data.focus3D.angle_x =mouseY;
			//trace(Scene_data.focus3D.angle_x);

			if (!Scene_data.ready ) return;
			var context : Context3D = Scene_data.context3D;
			context.clear(bgR, bgG, bgB, 1);
			projMatrix = new PerspectiveMatrix3D();
			projMatrix.perspectiveFieldOfViewRH(1, 1, 0.1, 10000);

			context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, projMatrix, true);
			context.setProgram(program);
			//particle.update();
			/*for(var i:int = 0;i<particlelist.length;i++){
				particlelist[i].update();
			}*/
			/*for each(particle in particlelist){
				particle.update(t);
			}*/
			particle.update(1);
			//baseparticle.update();
			context.present();
		}
		public function addView():void{
			new Label(this,50,0,"横排数量");
			new Label(this,150,0,"纵排数量");
			new Label(this,50,30,"播放速度");
			new Label(this,50,60,"选择贴图");
			new Label(this,50,150,"背景颜色");
			
			var parNum:Text = new Text(this,120,0,"3");
			parNum.addEventListener(Event.CHANGE,onNumChg);
			parNum.width = 20;
			parNum.height = 20;
			
			var parNum2:Text = new Text(this,200,0,"3");
			parNum2.addEventListener(Event.CHANGE,onNumChg2);
			parNum2.width = 20;
			parNum2.height = 20;
			
			var xh:HSlider = new HSlider(this,120,35,speedChg);
			xh.minimum = 1;
			xh.maximum = 20;
			xh.value = 5;
			
			var btn:PushButton = new PushButton(this,120,60,"选择文件",onClickTex);
			var col:ColorChooser = new ColorChooser(stage,120,150,0xff0000,colorChose);
			col.usePopup = true;
			var r1:RadioButton = new RadioButton(this,50,95,"SOURCE_ALPHA,ONE",true,xchg);
			var r2:RadioButton = new RadioButton(this,50,110,"ONE,ONE",false,ychg);
			var r3:RadioButton = new RadioButton(this,50,125,"SOURCE_ALPHA,ONE_MINUS_SOURCE_ALPHA",false,zchg);
			new Label(this,0,580,"verson：1");
			
		}
		private function onNumChg(event:Event):void{
			var num:int = int(event.target.text);
			this.wnum = num;
		}
		private function onNumChg2(event:Event):void{
			var num:int = int(event.target.text);
			this.hnum = num;
		}
		private function speedChg(event:Event):void{
			var num:Number = event.target.value;
			//particle.speed = num;
		}
		private function xchg(event:Event):void{
			Scene_data.context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE);
		}
		private function ychg(event:Event):void{
			Scene_data.context3D.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ONE);
		}
		private function zchg(event:Event):void{
			Scene_data.context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
		}
		private var bgR:Number = 0.5;
		private var bgG:Number = 0.5;
		private var bgB:Number = 0.5;
		private function colorChose(event:Event):void{
			var pixelValue:uint = event.target.value;
			var red:uint = pixelValue >> 16 & 0xFF;
			var green:uint = pixelValue >> 8 & 0xFF;
			var blue:uint = pixelValue & 0xFF;
			//particle.colorVec = new Vector.<Number>();
			//particle.colorVec.push(red/256,green/256,blue/256,0);
			bgR = red/256;
			bgG = green/256;
			bgB = blue/256;
		}
		private var file:FileReference
		private function onClickTex(event:Event):void{
			file = new FileReference();
			file.browse();
			file.addEventListener(Event.SELECT,onFileSel)
			file.addEventListener(Event.COMPLETE,onLoadCom);
		}
		private function onFileSel(event:Event):void{
			file.load();
		}
		private function onLoadCom(event:Event):void{
			var byte:ByteArray = event.target.data;
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onbitmap);
			loader.loadBytes(byte);
		}
		private function onbitmap(event:Event):void{
			var bitmapdata:BitmapData = Bitmap(event.target.content).bitmapData;
			//particle = new UVParticle(Scene_data.context3D);
			//particle.setWHnum(wnum,hnum);
			//particle.uploadTexture(bitmapdata);
		}


	}
}

